package com.ctcc.serviceImpl;

import com.ctcc.entity.count.User;
import com.ctcc.entity.count.User_;
import com.ctcc.repository.count.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * Created by Peanut on 2018/4/9.
 */
@Service
public class UserImpl {
    @Autowired
    UserRepository userRepository;
    public void  saveUser(User uesr){
        userRepository.save(uesr);
    }
    public User getUserByOpenId(String openId){
        return  userRepository.findOne(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.and(criteriaBuilder.equal(root.get(User_.openId), openId));
            }
        });
    }
}
